from fastapi import APIRouter, Depends, Form, HTTPException
from sqlalchemy.orm import Session
import models
from auth import pwd_context
from db import get_db
router = APIRouter(prefix="/api/root", tags=["超级用户"])
@router.put("/{user_id}")
def update_user(Root_id: int, name: str=Form(...), phone: str=Form(...),  password: str=Form(...),
                db: Session = Depends(get_db)):
    user = db.query(models.Root).filter(models.Root.id == Root_id).first()
    if Root_id is None:
        raise HTTPException(status_code=404, detail="用户不存在")
    if name:
        user.name = name
    if phone:
        user.phone = phone
    if password:
        user.password = pwd_context.hash(password)
    db.commit()
    db.refresh(user)
    return {"message": "用户信息更新成功", "user": user}


